package oop.ex3.serverprotocol;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import oop.ex3.nameserver.NameServer;
import oop.ex3.sharedclasses.IPaddress;

/**
 * Handles a ContainNameServer Message sent after a WELCOME Message.
 * Recieves the ContainNameServer message's NameServer IPaddress.
 * @author zablo
 *
 */
public class WelcomeContainNameServer extends ClientMessageHandler{
	private IPaddress _ipAddress;
	
	/**
	 * Returns IPaddress (IP+PORT) of this ContainNameServer Message.
	 * @return the IPaddress of this ContainNameServer Message.
	 */
	public IPaddress getIPadress(){
		return this._ipAddress;
	}
	
	/**
	 * Will handle a WelcomeContainFile Message.
	 * Will save the IP address + port in this instance.
	 */
	public boolean run(NameServer server, IPaddress sessionIP,
			DataInputStream clientInput, DataOutputStream clientOutput) {
	try {
		String ip=clientInput.readUTF();
		int port=clientInput.readInt();
		
		this._ipAddress=new IPaddress(ip, port);
		
		if (endsWithEnd(clientInput)==FAILURE){
			return FAILURE;
		}
	} catch (IOException e) {
		e.printStackTrace();
		return FAILURE;
	}
	
	return SUCCESS;	
	}

}
